package com.tbit.uqbike.service.business.impl;


import com.tbit.uqbike.constant.RedisConstant;
import com.tbit.uqbike.object.pojo.BrandRemoteConfig;
import com.tbit.uqbike.object.pojo.SmsAuthConfig;
import com.tbit.uqbike.service.base.RedisService;
import com.tbit.uqbike.service.business.SmsAuthConfigService;
import com.tbit.uqbike.webmanager.dao.core.SmsAuthConfigDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chen
 * @Description: 短信实名认证配置信息
 * @Date: 创建于 9:43 2019/8/8
 */
@Service("smsAuthConfigService")
public class SmsAuthConfigServiceImpl implements SmsAuthConfigService {
    @Autowired
    private SmsAuthConfigDao smsAuthConfigDao;
    @Autowired
    private RedisService redisService;

    @Override
    public SmsAuthConfig getByAccountId(Integer accountId) {
        return smsAuthConfigDao.getByAccountId(accountId);
    }

    @Override
    public void add(SmsAuthConfig smsAuthConfig) {
        SmsAuthConfig smsAuthConfigDB = getByAccountId(smsAuthConfig.getAccountId());
        if (smsAuthConfigDB != null) {
            if (smsAuthConfig.getRemark() == null) {
                smsAuthConfig.setRemark(smsAuthConfigDB.getRemark());
            }
            smsAuthConfigDao.edit(smsAuthConfig);
            /**清除redis数据*/
            redisService.del(RedisConstant.SMSCONFIG_KEY_PREFIX + smsAuthConfig.getAccountId());
        } else {
            smsAuthConfigDao.add(smsAuthConfig);
        }
    }

    @Override
    public BrandRemoteConfig getBrandRemoteConfig(Integer accountId, Integer configType) {
        Map<String, Integer> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("configType", configType);
        return smsAuthConfigDao.getByConfigType(map);
    }

    @Override
    public String getBrandRemoteConfigValue(Integer accountId, Integer configType) {
        BrandRemoteConfig brandRemoteConfig = getBrandRemoteConfig(accountId,configType);
        if(brandRemoteConfig==null){
            return null;
        }
        return brandRemoteConfig.getParamKV().substring(brandRemoteConfig.getParamKV().indexOf("=") + 1);
    }
}
